package core.jvm;

import java.util.Base64;

public class MyClassLoader extends ClassLoader {

    public static void main(String[] args) {
        try {
            // 加载并初始化hello类
            new MyClassLoader().findClass("core.jvm.Hello").newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    public Class<?> defineMyClass(byte[] b, int off, int len) {
        return super.defineClass(b, off, len);
    }


    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        String helloBase64 = "";

        byte[] bytes = decode(helloBase64);
        return defineClass(name, bytes, 0, bytes.length);
    }

    public byte[] decode(String base64) {
        return Base64.getDecoder().decode(base64);
    }


}
